//
// Created by lwj66 on 24-6-18.
//
#include "stdio.h"
typedef struct ListNode ln;

struct ListNode
{
    int val;
    struct ListNode *next;
};

/*/struct ListNode* removeElements(struct ListNode* head, int val)
{
    if(head == NULL)
        return NULL;

    head->next = removeElements(head->next, val);

    return head->val == val ? head->next : head;
}*/

struct ListNode* removeElements(struct ListNode* head, int val)
{
    if(head == NULL)
        return head;

    ln* headNode, *tailNode;
    headNode = tailNode = NULL;
    while (head != NULL)
    {
        if(head->val != val)
        {
            if(tailNode == NULL)
                headNode = tailNode = head;
            else
            {
                tailNode->next = head;
                tailNode = tailNode->next;
            }
        }
        head = head->next;
    }
    if(tailNode)
        tailNode->next = NULL;
    return headNode;
}